package com.example.administrator.foodrowfresh.fragmentframework;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import com.example.administrator.foodrowfresh.R;
import com.example.administrator.foodrowfresh.jiao.NetConfigPromotion;
import com.example.administrator.foodrowfresh.jiao.PromotionItemActivity;
import com.example.administrator.foodrowfresh.jiao.adapter.PromotionAdapter;
import com.example.administrator.foodrowfresh.jiao.modle.MyHttpUtils;
import com.example.administrator.foodrowfresh.jiao.modle.PromotionModel;
import com.example.administrator.foodrowfresh.superBase.MyBaseFragment;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/4/30.
 */
public class Promotion extends MyBaseFragment implements AbsListView.OnScrollListener, AdapterView.OnItemClickListener {

    private PullToRefreshListView mListView;
    private List<PromotionModel.DataBean.ActivityBean> mDatas;
    private BaseAdapter mAdapter;
    // 判断是否滑动到底部
    private boolean isScrollToBottom;
    private ILoadingLayout loadingLayoutProxy;

    private View mView;
    private int DETAIL_ID;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            List<PromotionModel.DataBean.ActivityBean> tempData = (List<PromotionModel.DataBean.ActivityBean>) msg.obj;
            if (tempData != null)
                mDatas.addAll(tempData);
            mAdapter.notifyDataSetChanged();
        }
    };


    @Override
    public View initView() {
        mView=LayoutInflater.from(getActivity()).inflate(R.layout.layout_promotion_fm,null,false);
        mListView = (PullToRefreshListView)mView.findViewById(R.id.pv_pull_text);
        mDatas = new ArrayList<>();
        //绑定监听
        mAdapter = new PromotionAdapter(mDatas,getActivity());

        mListView.setAdapter(mAdapter);
        //绑定监听
        mListView.setOnScrollListener(this);
        mListView.setOnItemClickListener(this);
        //子线程下载json
        downLoadJson(NetConfigPromotion.PROMOTION_PATH);
        return mView;
    }


    @Override
    public void initData() {

    }


    private void downLoadJson(final String URL) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String json = MyHttpUtils.getTextFromUrl(URL);
                Gson gson = new Gson();

                PromotionModel test = gson.fromJson(json, PromotionModel.class);
                List<PromotionModel.DataBean.ActivityBean> data = test.getData().getActivity();

                Message message = Message.obtain();
                message.obj = data;
                Log.d("1542", "数据" + data);
                handler.sendMessage(message);

            }
        }).start();
    }

    private void setData() {
        mListView.setMode(PullToRefreshBase.Mode.BOTH);
        loadingLayoutProxy = mListView.getLoadingLayoutProxy();
        loadingLayoutProxy.setRefreshingLabel("正在为你拼命加载");
        loadingLayoutProxy.setPullLabel("正在加载");
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(),PromotionItemActivity.class);
        startActivity(intent);
        mAdapter.notifyDataSetChanged();
    }

    //滑动到底部，处于停止状态执行分页加载
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (isScrollToBottom && scrollState == SCROLL_STATE_IDLE) {
            loadMore();
        }
    }
    //分页加载
    private void loadMore() {
        downLoadJson(NetConfigPromotion.PROMOTION_PATH);
    }

    //滑动监听是否到底部
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (firstVisibleItem + visibleItemCount == totalItemCount) {
            isScrollToBottom = true;
        } else {
            isScrollToBottom = false;
        }
    }

}
